<?php

/**
 * gallery actions.
 *
 * @package    hacride
 * @subpackage gallery
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class galleryActions extends sfActions
{
  public function executeIndex()
  {
     // on retrouve toutes les galleries photos faites
     $c = new Criteria();
     $c->addAscendingOrderByColumn(GalleryPeer::POSITION);
     $this->galleries = GalleryPeer::doSelect($c); 
    
     if ($this->getRequest()->getMethod() == sfRequest::POST)
     {
       
       $gallery = new Gallery();
       $gallery->setTitle($this->getRequestParameter('title'));
       $gallery->setIsPublished(false);
       $gallery->setPosition(0);
       $gallery->save();
       
       $galleryDir = 'gallery_'.$gallery->getId();
       
       $absCurrentDir = sfConfig::get('sf_web_dir').'/admin/uploads/galleries';
       
       if (!is_dir($absCurrentDir.'/'.$galleryDir))
       {
         // If the thumbnails directory doesn't exist, create it now
         $old = umask(0000);
         @mkdir($absCurrentDir.'/'.$galleryDir, 0777, true);
         umask($old);
         
         $old = umask(0000);
         @mkdir($absCurrentDir.'/'.$galleryDir.'/thumbnail', 0777, true);
         umask($old);
       }
       
       $this->setFlash('success', 'La gallerie a été créée', false);
       $this->redirect('gallery/index');
     }
  }
  
  public function executeTogglePublish()
  {
    $gallerie = GalleryPeer::retrieveByPK($this->getRequestParameter('gallery_id'));
    $this->redirectUnless($gallerie, 'gallery/index');
    
    $state = ($this->getRequestParameter('publish') == 'false') ? 0 : 1;
    
    $gallerie->setIsPublished($state);
    $gallerie->save();
    
    $this->redirect('gallery/index');
  }
  
  /**
   * supprime une gallerie photo
   */
  public function executeDelete()
  {
    $gallerie = GalleryPeer::retrieveByPK($this->getRequestParameter('gallery_id'));
    $this->redirectUnless($gallerie, 'gallery/index');
    
    // on supprime toutes les images associées
    $c = new Criteria();
    $c->add(PicturePeer::GALLERY_ID, $this->getRequestParameter('gallery_id'));
    PicturePeer::doPhysicDelete($c);
    PicturePeer::doDelete($c);

    // on supprime la gallery ainsi que le répertoire associé sur le disque
    $currentDir = 'gallery_'.$gallerie->getId();
    $absCurrentDir = sfConfig::get('sf_web_dir').'/admin/uploads/galleries/'.$currentDir;
    
    if (is_dir($absCurrentDir.'/thumbnail'))
    {
      @rmdir($absCurrentDir.'/thumbnail');
    }
    
    if (is_dir($absCurrentDir))
    {
      @rmdir($absCurrentDir);
    }
        
    $gallerie->delete();
    
    $this->setFlash('success', 'Gallerie supprimée', false);
    $this->redirect('gallery/index');
    
  }
  
  /**
   * affiche la page de trie des galleries
   */
  public function executeGalleryOrder()
  {
    $c = new Criteria();
    $c->addAscendingOrderByColumn(GalleryPeer::POSITION);
    $c->add(GalleryPeer::IS_PUBLISHED, true);
    $this->galleries = GalleryPeer::doSelect($c);
  }
  
  /**
   * sauvegarde l'ordre de trie des galleries
   *
   */
  public function executeSaveGalleryOrder()
  {
    $i = 0;
    foreach ($this->getRequestParameter('order') as $id)
    {
      $gallery = GalleryPeer::retrieveByPK($id);
      $gallery->setPosition($i);
      $gallery->save();
      $i++;
    }
    return sfView::NONE;
  }
  
  /**
   * sauvegarde l'ordre de trie des images
   *
   */
  public function executeSavePictureOrder()
  {
    $i = 0;
    foreach ($this->getRequestParameter('order') as $id)
    {
      $picture = PicturePeer::retrieveByPK($id);
      $picture->setPosition($i);
      $picture->save();
      $i++;
    }
    return sfView::NONE;
  }
  
  /**
   * affiche les miniatures des photos associés à une gallery
   */
  public function executeView()
  {

    $c = new Criteria();
    $c->add(PicturePeer::GALLERY_ID, $this->getRequestParameter('gallery_id'));
    $c->addAscendingOrderByColumn(PicturePeer::POSITION);
    $this->pictures = PicturePeer::doSelect($c);

    $this->gallery = GalleryPeer::retrieveByPK($this->getRequestParameter('gallery_id'));
    $this->forward404Unless($this->gallery);
  }
  
  /**
   * affiche la page d'upload des images
   */
  public function executeAddPictures()
  {
    $this->gallery = GalleryPeer::retrieveByPK($this->getRequestParameter('gallery_id'));
        
  }
  
  /**
   * éxecute l'upload des images pour une gallery donée
   */
  public function executeUpload()
  {
    $gallery = GalleryPeer::retrieveByPK($this->getRequestParameter('gallery_id'));
    
    if ($this->getRequest()->hasFiles())
    {
      foreach ($this->getRequest()->getFileNames() as $fileName)
      {
        // upload phsique
        
        $fileSize  = $this->getRequest()->getFileSize($fileName);
        $fileType  = $this->getRequest()->getFileType($fileName);

        $fileError = $this->getRequest()->hasFileError($fileName);
        $file = $this->sanitizeFile($this->getRequest()->getFileName($fileName));
        
        $uploadDir = sfConfig::get('sf_web_dir').'/admin/uploads/galleries/gallery_'.$gallery->getId();
        
        // creation de la miniature
        $thumbnail = new sfThumbnail(sfConfig::get('app_gallery_width'), sfConfig::get('app_gallery_height'), true, false, 100, 'sfGDAdapter');
        $thumbnail->loadFile($this->getRequest()->getFilePath($fileName));
        $thumbnail->save($uploadDir.'/thumbnail/'.$file);
        
        $this->getRequest()->moveFile('Filedata', $uploadDir.'/'.$file);
       
        
        list($name, $ext) = explode('.', $file);
        
        $picture = new Picture();
        $picture->setTitle($name);
        $picture->setPath($file);
        $picture->setGallery($gallery);
        $picture->save();
        
      }
    }
    
    die('Upload Successfull');

  }
  
  public function executeDeletePicture()
  {
    $gallerie = GalleryPeer::retrieveByPK($this->getRequestParameter('gallery_id'));
    $this->redirectUnless($gallerie, 'gallery/index');
    
    // on supprime toutes l'image
    $c = new Criteria();
    $c->add(PicturePeer::GALLERY_ID, $this->getRequestParameter('gallery_id'));
    $c->add(PicturePeer::ID, $this->getRequestParameter('id'));
    PicturePeer::doPhysicDelete($c);
    PicturePeer::doDelete($c);
    
    $this->redirect('gallery/view?gallery_id='.$gallerie->getId());
  }
  
  public function executeOrganize()
  {
    $c = new Criteria();
    $c->add(PicturePeer::GALLERY_ID, $this->getRequestParameter('gallery_id'));
    $c->addAscendingOrderByColumn(PicturePeer::POSITION);
    $this->pictures = PicturePeer::doSelect($c);

    $this->gallery = GalleryPeer::retrieveByPK($this->getRequestParameter('gallery_id'));
    $this->forward404Unless($this->gallery);
  }
  
  private function sanitizeDir($dir)
  {
    return preg_replace('/[^a-z0-9_-]/i', '_', $dir);
  }

  private function sanitizeFile($file)
  {
    return preg_replace('/[^a-z0-9_\.-]/i', '_', $file);
  }
  
}
